package com.qy.StudentInfo.service.impl;

import com.qy.StudentInfo.handler.MailHandler;
import com.qy.StudentInfo.mapper.GradeMapper;
import com.qy.StudentInfo.mapper.SelectCourseMapper;
import com.qy.StudentInfo.mapper.StudentMapper;
import com.qy.StudentInfo.mapper.TeacherMapper;
import com.qy.StudentInfo.pojo.*;
import com.qy.StudentInfo.service.MailService;
import com.qy.StudentInfo.service.SelectCourseService;
import com.qy.StudentInfo.utils.CommonUtil;
import com.qy.StudentInfo.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @program: student-info-master
 * @author: QiYue
 * @create: 20220429 16:19
 **/
@Service
public class MailServiceImpl implements MailService {
    static Logger log = Logger.getLogger(MailHandler.class);

    private CommonUtil commonUtil;
    @Autowired
    public void setCommonUtil(CommonUtil commonUtil) {
        this.commonUtil = commonUtil;
    }
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private SelectCourseMapper selectCourseMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 发送学生邮件
     * @param req
     */
    @Override
    public void sendMail(SendEmailReq req) {
        HashMap<String,Object> hashMap  = new HashMap<>();
        hashMap.put("sid",req.getSid());
        String studentBody="";
        // 发送邮件页面点击发送邮件，调用接口发送邮件，返回Boolean值，前端由ajaxForm接收
        List<Grade> gradeList = new ArrayList<>();

        List<Student> studentList = new ArrayList<>();
        studentList =studentMapper.getAllStudent(req.getSid());
        String studentName  = "";
        for (Student student:studentList) {
             studentName = "<h1>"+student.getSname()+"同学，您收到了本学期的成绩单！快来查看吧</h1>";
        }
        gradeList = gradeMapper.getEedCourseBySid(hashMap);
        for (Grade grade: gradeList) {
            String temp = "    <tr>\n" +
                    "        <td>"+grade.getCid()+"</td>\n" +
                    "        <td>"+grade.getCname()+"</td>\n" +
                    "        <td>"+grade.getGrade()+"</td>\n" +
                    "        <td>"+grade.getCredits()+"</td>\n" +
                    "    </tr>";
            studentBody+=temp;
        }
        System.out.println(Constants.STUDENT_HEAD+studentName+studentBody+Constants.STUDENT_TAIL);
        req.setEmailText(Constants.STUDENT_HEAD+studentName+studentBody+Constants.STUDENT_TAIL);
        commonUtil.sendEmail(req);
    }

    /**
     * 发送教师邮件
     * @param req
     */
    @Override
    public void sendTeacherMail(SendEmailReq req) {

        HashMap<String,Object> hashMap  = new HashMap<>();
        hashMap.put("cid",req.getCid());
        log.info("cid为:"+req.getCid());
        String studentBody="";
        // 发送邮件页面点击发送邮件，调用接口发送邮件，返回Boolean值，前端由ajaxForm接收
        //查询出教师的tid放入hashmap中
        List<Teacher> teacherTid = teacherMapper.getTeacherTid(req.getCid());
        HashMap<String,Object>hashMapTeacher  = new HashMap<>();
        hashMap.put("tid",teacherTid.get(0));
        List<Teacher> byTeacherTid = teacherMapper.getByTeacherTid(hashMapTeacher);
        String teacherName = "";
        for (Teacher teacher:byTeacherTid) {
            teacherName=teacher.getTname();
        }

        List<StuExitSelect> stuExitSelectList ;
        //查询所有选课名单
        stuExitSelectList =selectCourseMapper.getLookByTid(hashMapTeacher);
        String studentName  = "";
        for (StuExitSelect stuExitSelect:stuExitSelectList) {
            studentName = "<h2>"+teacherName+"老师您好!本学期您的课程为:"+stuExitSelect.getCname()+"总共可以选"+stuExitSelect.getTotalNum()+"人,一共选择"+stuExitSelect.getStuSum()+"人选择您的课程</h2>";
        }
        System.out.println(studentName);
        List<Student> studentList = new ArrayList<>();
        studentList =selectCourseMapper.getByStuSid(hashMap);
        for (Student student: studentList) {
            String temp = "    <tr>\n" +
                    "        <td>"+student.getSid()+"</td>\n" +
                    "        <td>"+student.getSname()+"</td>\n" +
                    "        <td>"+student.getSsex()+"</td>\n" +
                    "        <td>"+student.getClassr()+"</td>\n" +
                    "        <td>"+student.getProfession()+"</td>\n" +
                    "        <td>"+student.getCollege()+"</td>\n" +
                    "    </tr>";
            studentBody+=temp;
        }
//        System.out.println(Constants.CLASS_HEAD+studentBody+studentName+Constants.STUDENT_TAIL);
        req.setEmailText(Constants.CLASS_HEAD+studentBody+"</table>\n" +
                studentName+"</body>\n" +
                "</html>");
        commonUtil.sendEmail(req);
    }
}
